from collections import deque
from typing import List


class Solution:
    def isSelfCrossing(self, distance: List[int]) -> bool:
        # 添加4次距离为0的移动 (其中第1个元素会被直接pop掉不起作用)
        now = deque([0, 0, 0, 0, 0])

        n = len(distance)

        # 处理第1种情况
        i = 0
        while i < n:
            now.popleft()
            now.append(distance[i])
            if now[4] <= now[2]:
                break
            i += 1

        if i == n:
            return False

        # 处理第j次移动的情况
        if now[4] >= now[2] - now[0]:
            now[3] -= now[1]
        i += 1

        # 处理第2种情况
        while i < n:
            now.popleft()
            now.append(distance[i])
            if now[4] >= now[2]:
                break
            i += 1

        return i != n


if __name__ == "__main__":
    print(Solution().isSelfCrossing(distance=[2, 1, 1, 2]))  # True
    print(Solution().isSelfCrossing(distance=[1, 2, 3, 4]))  # False
    print(Solution().isSelfCrossing(distance=[1, 1, 1, 1]))  # True

    # 测试用例22/29
    print(Solution().isSelfCrossing(distance=[3, 3, 3, 2, 1, 1]))  # False
